﻿using BLL;
using DTO;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HDT.GUI
{
    public partial class FrmThemMH : Form
    {
        private string PRODUCT_IMAGE_FOLDER = "Image\\_Product\\";

        public delegate void MyDelegate();
        public MyDelegate refreshForm;
        private List<LoaiMatHangDTO> dsLoai;
        private Hashtable dsChietKhau;
        Image productImage;

        public FrmThemMH()
        {
            InitializeComponent();
            dsChietKhau = new Hashtable();
            picMH.Image = picMH.ErrorImage;
            productImage = null;
        }
        private void btnQuayLai_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnThemCK_Click(object sender, EventArgs e)
        {
            FrmThemCK frm_ThemCK = new FrmThemCK();
            frm_ThemCK.AddChietKhau = new FrmThemCK.MyDelegate(AddChietKhau);
            frm_ThemCK.ShowDialog();
        }

        private void FrmThemMH_Load(object sender, EventArgs e)
        {
            LoadLoaiMatHangInfo();
            picMH.Image = productImage;
        }

        private void LoadLoaiMatHangInfo()
        {
            try
            {
                dsLoai = new LoaiMatHangBLL().Select("TrangThai = 1");
                foreach (LoaiMatHangDTO loai in dsLoai)
                {
                    cboLoaiMH.Items.Add(loai.TenLoaiMatHang);
                }
                cboLoaiMH.SelectedIndex = 0;
            }
            catch { }
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            bool status = !("".Equals(txtTenMH.Text.Trim()) || "".Equals(txtDViTinh.Text.Trim()));
            btnThemMH.Enabled = status;
            btnThemCK.Enabled = status;
            if (!status)
            {
                dsChietKhau.Clear();
                ShowTyLeChietKhau();
            }
        }

        private void dgvCK_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (!"".Equals(dgvCK.SelectedRows[0].Cells[0].Value.ToString()))
                {
                    btnXoaCK.Enabled = true;
                }
            }
            catch
            {
                btnXoaCK.Enabled = false;
            }
        }

        private void AddChietKhau(int SoLuong, double TyLe)
        {
            if (dsChietKhau.ContainsKey(SoLuong))
            {
                DialogResult dialogResult = MessageBox.Show("Số lượng tối thiểu này đã được gán tỷ lệ. Bạn muốn cập nhật tỷ lệ này?", "CẢNH BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    dsChietKhau[SoLuong] = TyLe;
                    ShowTyLeChietKhau();
                }
            }
            else
            {
                dsChietKhau.Add(SoLuong, TyLe);
                ShowTyLeChietKhau();
            }
        }

        private void ShowTyLeChietKhau()
        {
            dgvCK.Rows.Clear();
            foreach (DictionaryEntry entry in dsChietKhau)
            {
                DataGridViewRow row = (DataGridViewRow)dgvCK.Rows[0].Clone();
                row.Cells[0].Value = entry.Key;
                row.Cells[1].Value = ((double)entry.Value).ToString("#,##0.00 %");
                dgvCK.Rows.Add(row);
            }
        }

        private void picMH_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
                if (open.ShowDialog() == DialogResult.OK)
                {
                    Bitmap bit = new Bitmap(open.FileName);
                    productImage = bit;
                }
            }
            catch
            {
                MessageBox.Show("Không thể mở được hình?", "LỖI", MessageBoxButtons.OK, MessageBoxIcon.Error);
                productImage = null;
            }
            picMH.Image = productImage;
        }

        private void btnXoaCK_Click(object sender, EventArgs e)
        {
            try
            {
                int SL = int.Parse(dgvCK.SelectedRows[0].Cells[0].Value.ToString());
                dsChietKhau.Remove(SL);
                ShowTyLeChietKhau();
            }
            catch { }
        }

        private void SaveProductImage(MatHangDTO matHang)
        {
            try
            {
                string path = PRODUCT_IMAGE_FOLDER + matHang.MaMatHang + ".bmp";
                productImage.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
                matHang.HinhAnh = matHang.MaMatHang + ".bmp";
                new MatHangBLL().Update(matHang);
            }
            catch
            {

            }
        }

        private void NewMatHang(ref MatHangDTO matHang)
        {
            matHang.TenMatHang = txtTenMH.Text.Trim();
            matHang.MaLoaiMatHang = dsLoai[cboLoaiMH.SelectedIndex].MaLoaiMatHang;
            matHang.ChiTiet = txtChiTiet.Text;
            matHang.DonViTinh = txtDViTinh.Text.Trim();
            new MatHangBLL().Insert(matHang);
            matHang = new MatHangBLL().GetLast();
        }

        private void SetGiaNiemYet()
        {
            GiaNiemYetDTO gia = new GiaNiemYetBLL().GetLast();
            gia.NgayApDung = DateTime.Now;
            gia.Gia = (int) txtGiaBan.Value;
            new GiaNiemYetBLL().Update(gia);
        }

        private void NewChietKhau(MatHangDTO matHang)
        {
            List<ChietKhauDTO> dsCK = new List<ChietKhauDTO>();
            foreach (DictionaryEntry entry in dsChietKhau)
            {
                ChietKhauDTO CK = new ChietKhauDTO();
                CK.MaMatHang = matHang.MaMatHang;
                CK.SoLuongToiThieu = short.Parse(entry.Key.ToString());
                CK.TyLeChietKhau = double.Parse(entry.Value.ToString());
                dsCK.Add(CK);
            }
            new ChietKhauBLL().Insert(dsCK);
        }

        private void btnThemMH_Click(object sender, EventArgs e)
        {
            try
            {
                MatHangDTO matHang = new MatHangDTO();
                SaveProductImage(matHang);                
                NewMatHang(ref matHang);                
                SetGiaNiemYet();
                NewChietKhau(matHang);
                MessageBox.Show("Thêm mặt hàng mới thành công", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                refreshForm();
                this.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "LỖI", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
